//
//  VisitorView.swift
//  MyLottery
//
//  Created by wujunjie on 2018/7/18.
//  Copyright © 2018年 wujunjie. All rights reserved.
//  访客视图

import UIKit
import SnapKit
protocol VisitorViewProtocol : NSObjectProtocol {
    func login()
    func regist()
}
/// MARK:- 创建一个 UIView 的子类
class VisitorView: UIView {
    ///代理 : 第一要用 weak 来修饰,第二要使用可选值
    weak var delegate : VisitorViewProtocol?
    ///大图标
    lazy var iconImage : UIImageView = UIImageView(named: "visitordiscover_feed_image_house")
    lazy var cycleImage :UIImageView = UIImageView(named: "visitordiscover_feed_image_smallicon")
    ///遮罩
    lazy var shadowImage : UIImageView = UIImageView(named: "visitordiscover_feed_mask_smallicon")
    ///文字
    lazy var textLabel : UILabel = UILabel(text: "在首页你可以追星,看黄段子,看美女,在首页你可以追星,看黄段子,看美女,在首页你可以追星,看黄段子,看美女,在首页你可以追星,看黄段子,看美女,在首页你可以追星,看黄段子,看美女,在首页你可以追星,看黄段子", alightment: .center, fontSize: 14)
    ///注册
    lazy var registButton : UIButton = UIButton(title: "注册", target:  self, action: #selector(didClickRegist), event: .touchUpInside)
    lazy var loginButton : UIButton = UIButton(title: "登录", target:  self, action: #selector(didClickLogin), event: .touchUpInside)
    init(){
        super.init(frame:kScreenBounds)
        setupUI()
        self.addCycleImageAnimation()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

///MARK:创建 UI
extension VisitorView{
    ///设置 UI:顺序是从上到下,从左往右
    private func setupUI(){
        self.backgroundColor = UIColor(white: 233/255.0, alpha: 1.0)
        
        ///添加子视图
        addSubview(iconImage)
        addSubview(cycleImage)
        addSubview(shadowImage)
        addSubview(textLabel)
        addSubview(registButton)
        addSubview(loginButton)
        ///设置子视图
        
        
        ///自动布局
        iconImage.snp.makeConstraints {(make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-100)
        }
        
        cycleImage.snp.makeConstraints { (make) in
            make.center.equalTo(iconImage)
        }
        
        shadowImage.snp.makeConstraints { (make) in
            make.centerX.equalTo(cycleImage)
            make.centerY.equalTo(cycleImage).offset(50)
            make.left.equalTo(self)
            make.right.equalTo(self)
        }
        
        textLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(cycleImage).offset(150)
            make.left.equalTo(self).offset(20)
            make.right.equalTo(self).offset(-20)
        }
        
        registButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self).offset(-100)
            make.centerY.equalTo(self).offset(150)
            make.size.equalTo(CGSize(width: 50, height: 40))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self).offset(100)
            make.centerY.equalTo(self).offset(150)
            make.size.equalTo(CGSize(width: 50, height: 40))
        }
    }
}

///MARK:点击事件
extension VisitorView{
    ///@objc 可以让 selector 以 oc 发消息的形式来调用该方法
    ///所有的点击事件,通知的时间,前面独家一个@objc privare 养成一个习惯
    ///注册
@objc   private func didClickRegist(){
        print("注册")
    ///在 swift 中调用代理的方法不需要判断代理是否存在,也不需要判断代理方法是否存在
        delegate?.regist()
    }
    ///登录
  @objc  private func didClickLogin(){
        print("登录")
         delegate?.login()
    }
}

extension VisitorView{
    ///添加动画
    private func addCycleImageAnimation(){
        let cycleAnimation = CABasicAnimation(keyPath: "transform.rotation")
        cycleAnimation.toValue = Double.pi * 2
        cycleAnimation.repeatCount = MAXFLOAT
        cycleAnimation.duration = 10
        cycleAnimation.isRemovedOnCompletion = false
        cycleImage.layer.add(cycleAnimation, forKey:"Basic")
    }
    
    ///设置 VisitorView
    func setupVisitorView(image:String,title:String,isAnimation:Bool){
        iconImage.image = UIImage(named: image)
        textLabel.text = title
        cycleImage.isHidden = !isAnimation
    }
}
